<template>
  <div ref="imageWrapper">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form-model ref="ruleForm" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="loadData">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="账套/核算单元" prop="unitCode">
                <a-input placeholder="点击选择" v-model="queryParam.accName" @click.native="openSearchModel"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-model-item label="期间" prop="unitCode">
                <a-input placeholder="点击选择" v-model="queryParam.time" @click.native="openSearchModel"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-model-item label="科目" prop="subject">
                <a-input placeholder="点击选择" v-model="queryParam.subject" @click.native="openSearchModel"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="凭证摘要" prop="">
                <a-input placeholder="点击选择" v-model="queryParam.summaryContent" @click.native="openSearchModel" />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <!-- <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button> -->
                <a-button
                  type="danger"
                  :loading="ydLoading"
                  @click="handleToYD(false)"
                  icon="arrow-right"
                  style="margin-left: 8px"
                  >转疑点</a-button
                >
                <a-button
                  :disabled="dataSource.length <= 0"
                  :loading="loadingExp"
                  type="primary"
                  style="margin-left: 8px"
                  icon="download"
                  @click="handleExportXls()"
                  >导出</a-button
                >
                <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                  返回
                </a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <!-- 操作按钮区域 -->
      <!--    <div class="table-operator">-->
      <!--      <a-button type="primary" @click="openSearchModel()">查询</a-button>-->
      <!--    </div>-->

      <div class="ant-alert ant-alert-info" style="margin: 16px 0">
        <span>集团凭证查询</span>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          @change="handleTableChange"
          :scroll="{ x: 2000, y: 'auto' }"
        >
          <template slot="summary" slot-scope="text">
            <j-ellipsis :value="text" :length="35" />
          </template>
          <span slot="action" slot-scope="text, record">
            <a @click="handleDetail(record)">关联</a>
            <a-divider type="vertical" />
            <a style="color: red" @click="handleToYD(record)">转疑点</a>
          </span>
        </a-table>
      </div>
      <!-- table区域-end -->

      <SearchModal ref="modalForm" @ok="onSearch" @clear="searchReset"></SearchModal>
      <YDModel ref="YDModel" :routerData="$route.path" />
      <VoucherDetail ref="VoucherDetail" />
    </a-card>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import SearchModal from './SearchModal'
import columns from './indexColumns'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from '@/views/sj/components/YDModel'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
import VoucherDetail from '@/views/sj/sjxmzygl/yzzx/jtpzcx/VoucherDetail'
import { postAction, getAction } from '@/api/manage'
export default {
  name: 'jtpzcx',
  mixins: [JeecgListMixin, base64toImg, jumpConfigMixin],
  components: {
    SearchModal,
    YDModel,
    VoucherDetail,
  },
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          this.$nextTick(() => {
            this.$refs.modalForm.model = Object.assign(this.queryParam, to.query)
            this.$refs.modalForm.handleOk()
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {
          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
        } else if (Object.keys(to.params).length) {
          if (to.params.from == 'kmmx') this.KmmxSearch(to.params)
          else if (to.params.from == 'fzmxz') this.FzmxzSearch(to.params)
        }
      },
      immediate: true,
    },
  },
  data() {
    return {
      queryParam: {},
      // 表头
      isBackButton: false,
      columns: columns(this),
      url: {
        list: '/sjyzzx/sjZxzyAccountVouche/queryAccountVoucheList',
        exportXlsUrl: '/sjyzzx/sjZxzyAccountVouche/exportXls',
      },
      loadingExp: false,
      disableMixinCreated: true,
    }
  },
  created() {},
  mounted() {
    // this.openSearchModel()
  },
  computed: {
    title() {
      return this.$route.meta.title
    },
  },
  methods: {
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.dataSource = []
      this.ipagination = this.$options.data().ipagination
    },
    onSearch(param) {
      debugger
      this.queryParam = Object.assign({}, param)
      if (!param.subjectCode) {
        this.queryParam.subject = ''
      }
      // console.log(this.queryParam,'accName');
      this.queryParam.time = `${param.startTime}~${param.endTime}`
      this.loadData()
    },
    openSearchModel() {
      this.$refs.modalForm.edit()
      this.$refs.modalForm.isCheck = false
    },
    handleToYD(record) {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        let ydnr = undefined
        if (record) {
          ydnr = `【凭证字号：${record.docCode}】【科目代码：${record.subjectCode}】【科目名称：${record.subjectName}】`
        }
        let time = `【${startTime}～${endTime}】【${this.title}表】`
        this.addYd(this.queryParam, ydnr, time)
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    handleDetail(record) {
      this.$refs.VoucherDetail.check(record)
    },

    // 科目明细帐查询
    KmmxSearch(params) {
      let { param, record, superParam } = params
      debugger
      this.onSearch(Object.assign({}, param, superParam, { summaryContent: record.summary }))
    },
    // 辅助明细账查询
    FzmxzSearch(params) {
      let { param, record } = params
      debugger
      this.onSearch(Object.assign({}, param, { summaryContent: record.summary }))
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() // 查询条件
      this.loading = true
      // this.dataSource=[]
      postAction(this.url.list, params).then((res) => {
        if (res.success) {
          // update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          // update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        } else {
          this.$message.error(res.message)
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleExportXls() {
      this.loadingExp = true
      getAction(this.url.exportXlsUrl, { ...this.queryParam }).then((res) => {
        this.$message.success('正在下载，请稍等！')
        let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
        // let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
        window.open(url, '_blank')
        this.loadingExp = false
      })
    },
  },
}
</script>
<style scoped>
.ant-select-dropdown {
  width: 300px;
}
</style>
<style lang='less' scoped>
@import '~@assets/less/common.less';
</style>